package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.mapper.TypeMapper;
import com.neuedu.pojo.Type;
import com.neuedu.service.ITypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author han
 * @since 2020-09-21
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements ITypeService {

    @Override
    public Object list(Type type) {
        // 如果 有name传过来 就按照name 模糊查询
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(type.getTypeName())) {
            wrapper.like("type_name", type.getTypeName());
        }

        // 如果分页返回 IPage 如果不分页 返回 List
        if(type.getWithPage() == 1) {
            return this.page(new Page<>(type.getPageNo(),type.getPageSize()),wrapper);
        } else {
            return this.list(wrapper);
        }
    }

    @Override
    public boolean add(Type type) {
        QueryWrapper<Type> wrapper = new QueryWrapper<>();
        wrapper.eq("type_name",type.getTypeName());
        Type query = this.getOne(wrapper);
        if(query == null )
            return this.save(type);
        return false;
    }

    @Override
    public boolean batchdel(Integer[] ids) {
        List<Integer> list = new ArrayList<>(ids.length);
        Collections.addAll(list,ids);
        Type type = new Type();
        type.setActive(0);
        UpdateWrapper<Type> wrapper = new UpdateWrapper<>();
        wrapper.in("id",list);
        return this.update(type,wrapper);
    }
}
